import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_shop/pages/base/base_state.dart';
import 'package:flutter_shop/pages/common/common_input_page/common_input_page.dart';
import 'package:flutter_shop/pages/member/member_router.dart';
import 'package:flutter_shop/pages/member/person_info/person_info_contract.dart';
import 'package:flutter_shop/pages/member/person_info/person_info_presenter.dart';
import 'package:flutter_shop/pages/member/select_dept/entity/dept_info.dart';
import 'package:flutter_shop/pages/member/select_people/entity/people_info.dart';
import 'package:flutter_shop/r.dart';
import 'package:flutter_shop/res/colors.dart';
import 'package:flutter_shop/res/dimens.dart';
import 'package:flutter_shop/res/styles.dart';
import 'package:flutter_shop/routers/navigator_utils.dart';
import 'package:flutter_shop/utils/utils.dart';
import 'package:flutter_shop/widget/common_app_bar.dart';
import 'package:flutter_shop/widget/load_image.dart';
import 'package:flutter_shop/widget/single_line_item.dart';
import 'package:flutter_shop/widget/tree_select/tree_node.dart';
import 'package:flutter_shop/widget/water_click.dart';

/// Title:个人信息页面 <br/>
/// Description: <br/>
/// author:xiemiao <br/>
/// date: 2020-03-16  <br/>
/// version 1.0 <br/>
class PersonInfoPage extends StatefulWidget {
  @override
  _PersonInfoPageState createState() => _PersonInfoPageState();
}

class _PersonInfoPageState extends BaseState<PersonInfoPage, PersonInfoPresenter> implements PersonInfoView {
  @override
  PersonInfoPresenter createPresenter() => PersonInfoPresenter();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: CommonAppBar(
        "个人信息",
        rightText: "保存",
        rightClick: () {
          mPresenter.savePersonInfo();
        },
      ),
      body: Container(
        child: ListView(
          children: <Widget>[
            _headImage(),
            SingleLineItem(
              leftText: "用户名",
              rightText: mPresenter.mForm.username,
              editable: false,
            ),
            SingleLineItem(
              leftText: "姓名",
              hint: "请输入",
              rightText: mPresenter.mForm.realname,
              onPressed: () {
                NavigatorUtils.goCommonInputPage(
                  context,
                  title: "姓名",
                  content: mPresenter.mForm.realname,
                  maxLength: 10,
                  inputType: CommonInputPage.INPUT_TYPE_ARR[0],
                  callback: (content) {
                    mPresenter.mForm.realname = content;
                    mPresenter.updateStateWidget();
                  },
                );
              },
            ),
            SingleLineItem(
              leftText: "手机",
              hint: "请输入",
              rightText: mPresenter.mForm.phone,
              onPressed: () {
                NavigatorUtils.goCommonInputPage(
                  context,
                  title: "手机",
                  content: mPresenter.mForm.phone,
                  maxLength: 11,
                  inputType: CommonInputPage.INPUT_TYPE_ARR[1],
                  callback: (content) {
                    mPresenter.mForm.phone = content;
                    mPresenter.updateStateWidget();
                  },
                );
              },
            ),
            SingleLineItem(
              leftText: "地址",
              hint: "请输入",
              rightText: mPresenter.mForm.address,
              onPressed: () {
                NavigatorUtils.goCommonInputPage(
                  context,
                  title: "地址",
                  content: mPresenter.mForm.address,
                  maxLength: 100,
                  inputType: CommonInputPage.INPUT_TYPE_ARR[0],
                  callback: (content) {
                    mPresenter.mForm.address = content;
                    mPresenter.updateStateWidget();
                  },
                );
              },
            ),
            SingleLineItem(
              leftText: "部门",
              hint: "请选择",
              rightText: mPresenter.mForm.deptName,
              onPressed: () {
                NavigatorUtils.pushResult<List<DeptInfo>>(
                  context,
                  MemberRouter.selectDept,
                  (result) {
                    mPresenter.mForm.deptId = Utils.joinFields(result, field: "Id");
                    mPresenter.mForm.deptName = Utils.joinFields(result, field: "Name");
                    mPresenter.updateStateWidget();
                  },
                );
              },
            ),
            SingleLineItem(
              leftText: "朋友",
              hint: "请选择",
              rightText: mPresenter.mForm.friendName,
              onPressed: () {
                NavigatorUtils.pushResult<List<PeopleInfo>>(
                  context,
                  MemberRouter.selectPeople,
                  (result) {
                    mPresenter.mForm.friendId = Utils.joinFields(result, field: "Id");
                    mPresenter.mForm.friendName = Utils.joinFields(result, field: "Name");
                    mPresenter.updateStateWidget();
                  },
                );
              },
            ),
          ],
        ),
      ),
    );
  }

  ///头像
  Widget _headImage() {
    return WaterClick(
      padding: Paddings.common,
      minHeight: 45,
      decoration: BoxDecoration(
        color: Colors.white,
        border: Border(
          bottom: BorderSide(
            width: 0.5,
            color: CommonColors.common_divide_line,
          ),
        ),
      ),
      onPressed: () {
        //选择图片
        mPresenter.chooseImage();
      },
      child: Row(
        children: <Widget>[
          Expanded(
            child: Container(
              child: Text(
                "头像",
                style: TextStyle(
                  color: CommonColors.common_text,
                  fontSize: CommonDimens.font_size_middle,
                ),
                maxLines: 1,
                overflow: TextOverflow.ellipsis,
              ),
            ),
          ),
          LoadImage(
            mPresenter.imgBytes ?? mPresenter.mForm.headPic,
            width: 50,
            height: 50,
            fit: BoxFit.cover,
            isClipOval: true,
          ),
          Gaps.h_gap_10,
          Image.asset(
            R.assetsImgCommonIconRightArrow,
            width: 17,
            height: 17,
          )
        ],
      ),
    );
  }
}
